﻿using SerialDemo.AdditionalAttributes;
using SerialDemo.Models;
using SerialDemo.Services;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SerialDemo.ViewModels
{
    public class MainViewModel:BindableBase
    {
        private ObservableCollection<Servo> _servos;

        public ObservableCollection<Servo> Servos
        {
            get { return _servos; }
            set { SetProperty(ref _servos, value); }
        }

        private string port;
        public string Port
        {
            get { return port; }
            set { SetProperty(ref port, value); }
        }

        private int baudRate;
        public int BaudRate
        {
            get { return baudRate; }
            set { SetProperty(ref baudRate, value); }
        }

        private ObservableCollection<string> ports;

        public ObservableCollection<string> Ports
        {
            get { return ports; }
            set { SetProperty(ref ports, value); }
        }

        private ObservableCollection<int> baudRates;

        public ObservableCollection<int> BaudRates
        {
            get { return baudRates; }
            set { SetProperty(ref baudRates, value); }
        }

        private bool isOpen=false;

        private string buttonText = "打开串口";

        public string ButtonText
        {
            get => buttonText;
            set => SetProperty(ref buttonText, value);
        }

        private SolidColorBrush _brush = Brushes.Gray;

        public SolidColorBrush Brush
        {
            get { return _brush; }
            set { SetProperty(ref _brush, value); }
        }


        private readonly ISerialPortService _serialPortService;

        private string dataToSend;

        public string DataToSend
        {
            get => dataToSend;
            set => SetProperty(ref dataToSend ,value);
        }

        private string receiveData;

        public string ReceiveData
        {
            get => receiveData;
            //是 BindableBase 类的一部分，通常用于视图模型（ViewModel）中，以支持数据绑定和属性更改通知
            set => SetProperty(ref receiveData , value); 
        }

        public DelegateCommand OpenPortCommand { get; private set; }
        public DelegateCommand<string>SendDataCommand { get; private set; }
        public DelegateCommand<object> SliderValueChangedCommand { get; private set; }
        public DelegateCommand<Servo> EnterCommand { get; private set; }

        public MainViewModel(ISerialPortService serialPortService)
        {
            _serialPortService = serialPortService;
            _serialPortService.DataReceived += SerialPortService_DataReceived;

            OpenPortCommand = new DelegateCommand(OpenPort);
            SendDataCommand = new DelegateCommand<string>(SendData);
            SliderValueChangedCommand = new DelegateCommand<object>(SendData);
            EnterCommand = new DelegateCommand<Servo>((val) =>
            {
                val.Angle = val.SetAngle;
            });

            string[] arr =_serialPortService.GetPort();
            if (arr.Length != 0)
            {
                Ports = new ObservableCollection<string>();
                foreach (var item in arr)
                {
                    Ports.Add(item);
                }
            }
            

            BaudRates = new ObservableCollection<int>
            {
                9600,
                38400,
                115200,
            };

            Servos = new ObservableCollection<Servo>
            {
                new Servo(0,"Motor"+0,0),
                new Servo(1,"Motor"+1,0),
                new Servo(2,"Motor"+2,0),
                new Servo(3,"Motor"+3,0),
                new Servo(4,"Motor"+4,0),
                new Servo(5,"Motor"+5,0),
                new Servo(6,"Motor"+6,0),
                new Servo(7,"Motor"+7,0),
                new Servo(8,"Motor"+8,0),
                new Servo(9,"Motor"+9,0),
                new Servo(10,"Motor"+10,0),
                new Servo(11,"Motor"+11,0),
                new Servo(12,"Motor"+12,0),
                new Servo(13,"Motor"+13,0),
                new Servo(14,"Motor"+14,0),
                new Servo(15,"Motor"+15,0)
            };
        }

        private void SerialPortService_DataReceived(object? sender, string data)
        {
            ReceiveData = data;
        }

        public void OpenPort()
        {
            isOpen = _serialPortService.GetStatus();
            if (isOpen)
            {
                _serialPortService.Close();
                isOpen = false;
                ButtonText = "打开串口";
                Brush = Brushes.Gray;
            }
            else
            {
                try
                {

                    _serialPortService.Open(Port, BaudRate);
                    isOpen = true;
                    ButtonText = "关闭串口";
                    Brush = Brushes.Green;

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
           
            
        }

        public void SendData(object obj)
        {
            if(obj is Slider slider)
            {
                string data = $"#{slider.GetValue(SliderBehavior.IdProperty)}P{slider.Value}T";
                _serialPortService.Write(data);
            }
               

        }
    }
}
